package ru.kipriz.todoly.api.dao.http;

import ru.kipriz.todoly.api.AuthenticationException;
import ru.kipriz.todoly.api.util.Base64;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Properties;

/**
 * <p>Copyright: Copyright (c) 2010</p>
 *
 * @author Andrey Volkov
 *         <br/>Date: 12/13/10
 */
public class WebRequest
{
    protected static String apiLink;
    protected static boolean authenticated;
    static {
        apiLink = "https://todo.ly/api/";
        authenticated = false;
    }

    public enum Method
    {
        GET("GET"), POST("POST"), DELETE("DELETE");

        private String method;

        Method(String method)
        {
            this.method = method;
        }

        @Override
        public String toString()
        {
            return method;
        }
    }

    protected static String encodedUser;

    public static boolean authenticate(String login, String password) throws UnknownHostException {
        String usernamePassword = login + ":" + password;
        encodedUser = Base64.encodeBytes(usernamePassword.getBytes());
        String result;
        try {
            result = request("authentication/isauthenticated.json", Method.GET, "");
            authenticated = (result != null && result.equalsIgnoreCase("true"));
            return authenticated;
        } catch (UnknownHostException uhe) {
            throw uhe;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String get(String url) throws IOException {
        if (!authenticated) {
            throw new AuthenticationException();
        }
        return request(url, Method.GET, "");
    }

    public static String delete(String url) throws IOException
    {
        if (!authenticated) {
            throw new AuthenticationException();
        }
        return request(url, Method.DELETE, "");
    }

    public static String post(String url, String content) throws IOException
    {
        if (!authenticated) {
            throw new AuthenticationException();
        }
        return request(url, Method.POST, content);
    }

    protected static String request(String url, Method method, String content)
            throws IOException
    {
        if (encodedUser == null)
        {
            throw new AuthenticationException();
        }
        HttpsURLConnection connection = (HttpsURLConnection) new URL(apiLink + url).openConnection();
        connection.setRequestProperty("Authorization", "Basic " + encodedUser);
        connection.setRequestProperty("charset", "UTF-8");
        connection.setRequestMethod(method.toString());
        if (method == Method.POST)
        {
            connection.setDoOutput(true);
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(content.getBytes());
        }
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader reader = new BufferedReader(inputStreamReader);
        String line;
        StringBuffer buffer = new StringBuffer();
        while ((line = reader.readLine()) != null)
        {
            buffer.append(line);
        }
        connection.disconnect();
        return buffer.toString();
    }
}
